package com.sdry.web.controller.y;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.sdry.model.y.Artificial;
import com.sdry.model.y.Share;
import com.sdry.model.zc.ZcSysUserEntity;
import com.sdry.service.y.ArtificialService;
import com.sdry.service.y.ShareService;
import com.sdry.utils.ExcelUtils;

@Controller
@RequestMapping("/share")
public class ShareController {
	 @Resource
	 ShareService shareService;

	 @Resource
	 ArtificialService artificialService;
	 public List<String> namess() {
         List<String> list = new ArrayList<>();
         list.add("序号");//0
         list.add("仓库");//1
         list.add("明细");//2
         list.add("金额");//3
         return list;
     }
  	 @RequestMapping("/uplodeFile")
     public void importor(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request, HttpServletResponse response,HttpSession session){
     	ZcSysUserEntity staf = (ZcSysUserEntity) session.getAttribute("user");
     
     	Date now = new Date();
 		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         String savePath = request.getSession().getServletContext().getRealPath("/WEB-INF/"+file.getOriginalFilename());
         File newFile=new File(savePath);
         //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
         try {
             file.transferTo(newFile);
         } catch (IOException e) {
             e.printStackTrace();
         }
         //取文件后缀
         String subfix = savePath.lastIndexOf(".")==-1? "" : savePath.substring(savePath.lastIndexOf(".")+1);
         System.out.println(subfix);
         List<List<String>> lists = new ArrayList<>();
         //获取导入行数
         List<String> names = namess();
         int num = names.size();
         if(subfix.equals("xls")){
             try {
                 lists = ExcelUtils.readXls(savePath, request, num);
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }else if(subfix.equals("xlsx")){
             try {
                 lists = ExcelUtils.readXlsx(savePath, request,num);
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }else{
             try {
                 response.getWriter().print("0");
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }

         int flag = 1;
         if (lists != null&&lists.size()>0) {
             for (int i = 0; i < lists.size(); i++) {
                 List<String> list = new ArrayList<>();
                 list= lists.get(i);
                 Share share=new Share();
                
                 share.setCreater(staf.getId());
                 share.setCtime(dateFormat.format(now));
                 if(null != list.get(0)) {
                	 share.setOnum(list.get(0));
                 }else {
                	 share.setOnum("");
                 }
                 if (null != list.get(1)) {
                    	 Long id = artificialService.queryWareByName(list.get(1).trim());
                         share.setWarehouseid(id);
                     } else {
                   	  share.setWarehouseid((Long) null);
                  }
                 if(null != list.get(2)) {
                	 share.setDetail(list.get(2));
                 }else {
                	 share.setDetail("");
                 }  
                 if(null != list.get(3)) {
                	 share.setMoney(new BigDecimal(list.get(3)));
                 }else {
                	 share.setMoney(new BigDecimal(0));
                 }  
                    long affact = shareService.addShare(share);
                    share=null;
                 if (affact == 0) {
                     flag = 0;
                     break;
                 }
                 }
             }
      
         try {
             response.getWriter().print(flag);
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
}
